package com.test.cordova.lifecycle;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;

import com.test.cordova.citylist.bean.City;

import java.util.List;


/**
 * LifecycleObserver
 */

public class MyLifecycleObserver implements LifecycleObserver {

    private static final String TAG = MyLifecycleObserver.class.getSimpleName();
    private Callback callback ;
    private Context context;
    private MainViewModel mainViewModel ;
    private LifecycleOwner lifecycleOwner ;

    public MyLifecycleObserver(Context context, LifecycleOwner lifecycleOwner, MainViewModel mainViewModel, Callback callback) {
        this.callback = callback;
        this.context = context;
        this.lifecycleOwner = lifecycleOwner;
        this.mainViewModel = mainViewModel;
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void start() {
        Log.e(TAG, "start");
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreate(){
        Log.e(TAG, "create");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void connectListener(){
        Log.e(TAG,"resume");
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void disconnectListener(){
        Log.e(TAG,"pause");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void stop() {
        Log.e(TAG,"stop");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void destory() {
        Log.e(TAG,"destroy");
    }

    public void requestData(){
        RequestCityCallback cityCallback = (RequestCityCallback) callback;
        mainViewModel.getCitys().observe(lifecycleOwner, new Observer<List<City>>() {
            @Override
            public void onChanged(@Nullable List<City> cities) {

                mainViewModel.getHotcitys().observe(lifecycleOwner, new Observer<List<String>>() {
                    @Override
                    public void onChanged(@Nullable List<String> strings) {

                        cityCallback.requestResult(cities,strings);

                    }
                });
            }
        });
    }

    public void requestUsers() {
        mainViewModel.getUser().observe(lifecycleOwner, new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable List<User> users) {

            }
        });
    }

    public void requestTitle() {
        mainViewModel.getTitle().observe(lifecycleOwner, new Observer<List<String>>() {
            @Override
            public void onChanged(@Nullable List<String> strings) {

            }
        });
    }

}
